//
//  DetailMainViewController.m
//  MirrorCharge
//
//  Created by dllo on 16/10/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DetailMainViewController.h"
#import "DJDetailImageTableViewCell.h"
#import "Masonry.h"
#import "ImageRotate.h"
#define kTableViewResue @"resue"
#define kTableViewImageResue @"resueImage"
@interface DetailMainViewController ()<UITableViewDelegate, UITableViewDataSource>{
      NSMutableArray *_allArray;//创建一个数据源数组
      NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开
}
@property(nonatomic, strong)NSMutableArray *sectionArray;
@property (nonatomic, strong)NSString *str;
@property (nonatomic, strong)UIButton *buttonYellowAdd;
@property (nonatomic, strong)UIView *viewRed;
@property (nonatomic, strong)UIBarButtonItem *item1;
@property (nonatomic, strong)UIBarButtonItem *item2;
@property (nonatomic, strong)UIImageView *imageViewRight;

@end

@implementation DetailMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [self createTableView];
      [self yellowButtonAdd];
      [self SegmentView];
      _item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Unknown"] style:UIBarButtonItemStylePlain target:self action:@selector(handleMoreRight:)];
      _item2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Unknown-1"] style:UIBarButtonItemStylePlain target:self action:@selector(handleSreachRight:)];
      self.navigationItem.rightBarButtonItems = @[_item1, _item2];
      dic = [NSMutableDictionary dictionary];
      _allArray = [@[@[@"工资薪水",@"早餐"],@[@"as_search_not_fund"],@[@"早餐"]]mutableCopy];
      
}
- (void)handleMoreRight: (UIBarButtonItem *)right{
      NSLog(@"更多");
}
- (void)handleSreachRight :(UIBarButtonItem *)right {
      NSLog(@"搜索");
}
- (void)createTableView {
      _tableView.delegate = self;
      _tableView.dataSource = self;
      [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewResue];
      [_tableView registerNib:[UINib nibWithNibName:@"DJDetailImageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewImageResue];
      _sectionArray = [NSMutableArray arrayWithObjects:@"First",@"Second", nil];
      _tableView.bounces = NO;
}
- (void)yellowButtonAdd {
      _buttonYellowAdd = [[UIButton alloc]initWithFrame:CGRectMake(self.view.center.x, self.view.bounds.size.height - 60, 50, 50)];
      [self.view addSubview:_buttonYellowAdd];
      [_buttonYellowAdd setImage:[UIImage imageNamed:@"QQ20161018-1"] forState:UIControlStateNormal];
}
- (void)SegmentView {
      UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"明细", @"流向"]];
      segment.frame = CGRectMake(self.view.center.x - 20, 8, 100, 25);
      self.navigationItem.titleView = segment;
      segment.selectedSegmentIndex = 0;
      segment.tintColor = [UIColor whiteColor];
      segment.backgroundColor = [UIColor redColor];
      [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
}
- (void)change:(UISegmentedControl *)segment {
      switch (segment.selectedSegmentIndex) {
                  case 0:
                  [self.viewRed removeFromSuperview];
                  self.navigationItem.rightBarButtonItems = @[_item1, _item2];
                  break;
                  case 1:
                  _viewRed = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
                  _viewRed.backgroundColor = [UIColor greenColor];
                  [self.view addSubview:self.viewRed];
                  self.navigationItem.rightBarButtonItems = nil;
                  break;
            default:
                  break;
      }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      NSString *string = [NSString stringWithFormat:@"%ld",section];
         if ([dic[string] integerValue] == 1 ) {  //打开cell返回数组的count
            NSArray *array = [NSArray arrayWithArray:_allArray[section]];
            if (section == 0) {
                  
            }
            return array.count;
      }else{
            return 0;
      }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
      if (indexPath.section == 1) {
            return 80;
      }
      return 35;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
      return _allArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
      return 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
      UIView *view = [UIView new];
      //创建一个手势进行点击，这里可以换成button
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_tap:)];
      view.tag = 300 + section;
      [view addGestureRecognizer:tap];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 35, 40, 40)];
      [view addSubview:label];
      UIView *viewgray = [[UIView alloc]initWithFrame:CGRectMake(0, 79, self.view.bounds.size.width, 1)];
      [view addSubview:viewgray];
      viewgray.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
      label.text = @"2016";
      label.font = [UIFont systemFontOfSize:14];
      label.textColor = [UIColor grayColor];
      UILabel *LabelYue = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
      [view addSubview:LabelYue];
      UIView *viewRed = [[UIView alloc]init];
      [view addSubview:viewRed];
      [viewRed mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label.mas_right).mas_offset(10);
            make.top.equalTo(label.mas_top).mas_offset(10);
            make.width.mas_offset(200);
            make.height.mas_offset(25);
      }];
      viewRed.backgroundColor = [UIColor redColor];
      UILabel *labelMani = [[UILabel alloc]init];
      [viewRed addSubview:labelMani];
      [labelMani mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_offset(0);
            make.left.mas_offset(5);
            make.width.mas_offset(100);
      }];
      labelMani.textColor = [UIColor whiteColor];
      labelMani.text = @"支 ￥1355.0";
      labelMani.font = [UIFont systemFontOfSize:14];
      UIView * viewGreen = [[UIView alloc]init];
      [view addSubview:viewGreen];
      viewGreen.backgroundColor = [UIColor greenColor];
      [viewGreen mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label.mas_right).mas_offset(10);
            make.bottom.mas_equalTo(viewRed.mas_top);
            make.width.mas_offset(50);
            make.height.mas_equalTo(viewRed.mas_height);
      }];
      UILabel *labelGreen = [[UILabel alloc]init];
      [viewGreen addSubview:labelGreen];
      [labelGreen mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_offset(0);
            make.left.mas_offset(5);
            make.width.mas_offset(100);
      }];
      labelGreen.text = @"收 ￥200.0";
      labelGreen.textColor = [UIColor whiteColor];
      labelGreen.font = [UIFont systemFontOfSize:14];
      _imageViewRight = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Unknown-2"]];
      [view addSubview:_imageViewRight];
      [_imageViewRight mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset( - 10);
            make.centerY.mas_offset(0);
            make.width.mas_offset(30);
            make.height.mas_offset(30);
      }];
      if (section == 0) {
            LabelYue.text = @"10";
      }else if (section == 1) {
            LabelYue.text = @"09";
            viewRed.backgroundColor = [UIColor whiteColor];
            labelMani.textColor = [UIColor redColor];
            viewGreen.backgroundColor = [UIColor whiteColor];
            labelGreen.textColor = [UIColor greenColor];
            labelMani.text = @"支 ￥0.00";
            labelGreen.text = @"收 ￥0.00";
      }else {
            LabelYue.text = @"08";
            viewGreen.backgroundColor = [UIColor whiteColor];
            labelGreen.textColor = [UIColor greenColor];
            labelGreen.text = @"收 ￥0.00";
            labelMani.text = @"支 ￥500.0";
            [viewRed mas_makeConstraints:^(MASConstraintMaker *make) {
                  make.left.mas_equalTo(label.mas_right).mas_offset(10);
                  make.top.equalTo(label.mas_top).mas_offset(10);
                  make.width.mas_offset(60);
                  make.height.mas_offset(25);
            }];
      }
      return view;
}
- (void)action_tap:(UIGestureRecognizer *)tap{
      _str = [NSString stringWithFormat:@"%ld",tap.view.tag - 300];
      if ([dic[_str] integerValue] == 0) {//如果是0，就把1赋给字典,打开cell
            [dic setObject:@"1" forKey:_str];
                }else{//反之关闭cell
            [dic setObject:@"0" forKey:_str];
            _imageViewRight.image = [ImageRotate image:_imageViewRight.image rotation:UIImageOrientationLeft];
      }
      // [self.tableView reloadData];
      _imageViewRight.image = [ImageRotate image:_imageViewRight.image rotation:UIImageOrientationRight];
      [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[_str integerValue]] withRowAnimation:UITableViewRowAnimationFade];//有动画的刷新
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      [tableView deselectRowAtIndexPath:indexPath animated:YES];
      if ([dic[_str]integerValue] == 0) {
            CGImageRef cgRef = _imageViewRight.image.CGImage;
            _imageViewRight.image = [UIImage imageWithCGImage:cgRef scale:1 orientation:UIImageOrientationRight];
      }else {
      
      }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      if (indexPath.section == 1) {
            DJDetailImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewImageResue forIndexPath:indexPath];
            cell.ImageViewBounds.image = [UIImage imageNamed:_allArray[indexPath.section][indexPath.row]];
            return  cell;
      }
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewResue forIndexPath:indexPath];
      cell.textLabel.text = _allArray[indexPath.section][indexPath.row];
      return cell;
      
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
